package cn.good.yan.config.security;


import cn.good.yan.entity.Admin;
import cn.good.yan.entity.User;
import cn.good.yan.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * Security配置类
 *
 * @author shengren.yan
 * @create 2024/9/15
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 用户Server
    @Autowired
    private UserService adminService;


    // 定义密码加密规则
    @Bean
    public PasswordEncoder getPw() {
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                // 允许这些方法的请求 方式1： 还可以重新 configure(WebSecurity web) 这个配置实现
                // 目前这里，允许任何请，拦截请求在网关这里实现
                .antMatchers("/**").permitAll()
                // 剩下的所有请求都必须认证，登录才能访问
                .anyRequest().authenticated()
                .and()
                // 使用JWT，不需要csrf
                .csrf().disable()
                .cors();
    }


    // 重写 UserDetailsService ，登录逻辑
    @Override
    @Bean
    public UserDetailsService userDetailsService() {
        return username -> {
            User user = adminService.getAdminByUserName(username);
            if (null != user) {
                Admin admin = new Admin();
                BeanUtils.copyProperties(user, admin);
                return admin;
            }
            return null;
        };
    }

    // 自己的登录逻辑
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // Security 执行我们自己重写的逻辑
        auth.userDetailsService(userDetailsService()).passwordEncoder(getPw());

    }


    // 允许这些方法的请求,也可以在下面的 .antMatchers("/login", "/logout").permitAll() 进行处理
    // /capt  验证码接口
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers(
//                "/login",
//                "/logout",
//                "/capt",
//                "/addd",
//                "/hello2",
//                "/css/**",
//                "/js/**",
//                "/index.html",
//                "favicon.ico",
//                "/doc.html",
//                "/webjars/**",
//                "/swagger-resources/**",
//                "/v2/api-docs/**"
//
//        );
//    }


}
